package com.yanqu.road.server.cmd.union.cross.honors;

import com.yanqu.road.entity.union.honors.UnionHistoryHonorsRecordData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.CrossUnionHonorsProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.C_UNION_GET_UNION_HISTORY_UNION_NAMES, desc = "获取历史荣誉-参与商会名单，查分组榜单")
public class CrossUnionHistoryHonorsRecordUnionDetailCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();

        if(context.getRet() != 0){
            return;
        }
        CrossUnionHonorsProto.UnionHistoryHonorsRecordUnionDetailReqMsg reqMsg = CrossUnionHonorsProto.UnionHistoryHonorsRecordUnionDetailReqMsg.parseFrom(packet.getBytes());

        CrossUnionHonorsProto.UnionHistoryHonorsRecordUnionDetailRespMsg.Builder respMsg = CrossUnionHonorsProto.UnionHistoryHonorsRecordUnionDetailRespMsg.newBuilder();
        respMsg.setRet(0);
        UnionHistoryHonorsRecordData result = null;
        List<UnionHistoryHonorsRecordData> list = CrossUnionHonorsMgr.getHistoryHonorsRecordList(context.getUnionInfo().getUnionUid());
        for (int i = 0; i < list.size(); i++) {
            UnionHistoryHonorsRecordData recordData = list.get(i);
            if(recordData.getActivityId() == reqMsg.getActivityId()){
                result = recordData;
                break;
            }
        }
        if(result != null){
            respMsg.addAllUnionName(StringUtils.stringToStringList(result.getUnionNames(), "@#\\$"));
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_GET_UNION_HISTORY_UNION_NAMES, respMsg));
    }

    @Override
    public void sendData() {

    }
}
